package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import components.Arc;
import components.Coordinate;
import components.Node;
import components.NodeList;

import languages.Language;

public class TabInsertArcMiliseconds extends JPanel implements ActionListener{

	private JTextField sLat,sLon;
	private JTextField eLat,eLon;
	private JTextArea breakCords;
	//?
	private JTextField three,four,five;
	
	private JButton add,clear;
	
	private JPanel panel, inPanel;
	
	private Window parent;
	private Language lang;
	
	public TabInsertArcMiliseconds(Language lang, Window parent){
		this.parent = parent;
		this.lang = lang;
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		createGUI();
	}

	private void createArc(){
		NodeList nodes = parent.getNodeList();
		Arc newArc = new Arc();
		int sNodeId;
		int eNodeId;
		ArrayList<Coordinate> breakCords = new ArrayList<Coordinate>();
		
		newArc.setLine(parent.getArcList().getArcs().get(parent.getArcList().getArcs().size()-1).getLine()+1);
		newArc.setFive(Integer.parseInt(this.five.getText()));
		newArc.setFour(Integer.parseInt(this.four.getText()));
		newArc.setThree(Integer.parseInt(this.three.getText()));
		
		sNodeId = nodes.findNode(Coordinate.milisecondsToDegrees(Integer.parseInt(this.sLat.getText())), Coordinate.milisecondsToDegrees(Integer.parseInt(this.sLon.getText())));
		eNodeId = nodes.findNode(Coordinate.milisecondsToDegrees(Integer.parseInt(this.eLat.getText())), Coordinate.milisecondsToDegrees(Integer.parseInt(this.eLon.getText())));
		
		if(sNodeId > 0){
			newArc.setStartNode(nodes.getNodeById(sNodeId));
		}
		else{
			Node sNode = new Node(nodes.getNodes().get(nodes.getNodes().size()-1).getId()+1,Coordinate.milisecondsToDegrees(Integer.parseInt(this.sLon.getText())), Coordinate.milisecondsToDegrees(Integer.parseInt(this.sLat.getText())),0,nodes.getNodes().get(nodes.getNodes().size()-1).getLine()+1);
			nodes.getNodes().add(sNode);
			newArc.setStartNode(sNode);
		}
		if(eNodeId > 0){
			newArc.setEndNode(nodes.getNodeById(eNodeId));
		}
		else{
			Node eNode = new Node(nodes.getNodes().get(nodes.getNodes().size()-1).getId()+1,Coordinate.milisecondsToDegrees(Integer.parseInt(this.eLon.getText())), Coordinate.milisecondsToDegrees(Integer.parseInt(this.eLat.getText())),0,nodes.getNodes().get(nodes.getNodes().size()-1).getLine()+1);
			nodes.getNodes().add(eNode);
			newArc.setStartNode(eNode);
		}
		
		if(!this.breakCords.getText().isEmpty()){
			String[] lines = this.breakCords.getText().split("\n");
			if(lines.length > 0){
				for(int i=0; i<lines.length;i++){
					//lat lon
					String[] cords = lines[i].split(",");
					if(cords.length > 0){
						boolean both = false;
						float first=-1,second=-1;
						for(int j=0; j<cords.length;j++){
							if(!cords[j].isEmpty()){
								if(first < 0)
									first = Coordinate.milisecondsToDegrees(Integer.parseInt(cords[j].trim()));
								if(second < 0){
									second = Coordinate.milisecondsToDegrees(Integer.parseInt(cords[j].trim()));
									both = true;
									break;
								}
							}
						}
						breakCords.add(new Coordinate(first,second));
					}
				}
			}
			
			newArc.setBreakCount(breakCords.size());
			newArc.setBreakCords(breakCords);
		}
		
		this.parent.getArcList().getArcs().add(newArc);
		
		this.parent.getArcLayer().addArc(newArc);
	}
	
	private void createGUI() {
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		inPanel = new JPanel();
		inPanel.setLayout(new GridLayout(4,1));
		
		inPanel.add(new JLabel(lang.tiASLat()));
		sLat = new JTextField();
		inPanel.add(sLat);
		
		inPanel.add(new JLabel(lang.tiASLon()));
		sLon = new JTextField();
		inPanel.add(sLon);
		
		panel.add(inPanel, BorderLayout.WEST);
		
		inPanel = new JPanel();
		inPanel.setLayout(new GridLayout(4,1));
		
		inPanel.add(new JLabel(lang.tiAELat()));
		eLat = new JTextField();
		inPanel.add(eLat);
		
		inPanel.add(new JLabel(lang.tiAELon()));
		eLon = new JTextField();
		inPanel.add(eLon);
		
		panel.add(inPanel, BorderLayout.EAST);
		
		inPanel = null;
		
		this.add(panel);
		
		panel = null;
		
		this.add(new JLabel(lang.tiA3()));
		three = new JTextField();
		this.add(three);
		this.add(new JLabel(lang.tiA4()));
		four = new JTextField();
		this.add(four);
		this.add(new JLabel(lang.tiA5()));
		five = new JTextField();
		this.add(five);
		
		this.add(new JLabel(lang.tiBreakCords()));
		breakCords = new JTextArea();
		breakCords.setRows(4);
		this.add(breakCords);
		
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		add = new JButton(lang.tiAdd());
		clear = new JButton(lang.tiClear());
		panel.add(add,BorderLayout.WEST);
		panel.add(clear,BorderLayout.EAST);
		
		add.setActionCommand("ADD");
		clear.setActionCommand("CLEAR");
		add.addActionListener(this);
		clear.addActionListener(this);
		
		this.add(panel);
		
	}

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		
		System.out.println(command);
		
		if("ADD".equals(command)){
			createArc();
		}
		else if("CLEAR".equals(command)){
			sLat.setText("");
			sLon.setText("");
			eLat.setText("");
			eLon.setText("");
			three.setText("");
			four.setText("");
			five.setText("");
			breakCords.setText("");
		}
	}
	
}
